<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan(13040@qq.com)
 * @Last Modified time: 2021-01-10 22:41:36
 */

namespace app\admin\model;
use app\common\model\BaseModel;

class SystemConfig extends BaseModel {
	// 设置数据表（不含前缀）
	//protected $name = '';

	// 定义时间戳字段名
	protected $createTime = '';
	protected $updateTime = '';

	public function getCacheConfigsBySpace($config_space) {
		$sys_identify = 'system_config';
		$configInfo = cache($sys_identify);
		if (empty($configInfo)) {
			$data = $this->where(array('config_space'=>$config_space))->select();
			$data = $data->toArray(); //转换为数组
			$list = array();
			foreach ($data as $key => $value) {
				$list[$value['config_name']] = $value['config_value'];
			}
			if ($config_space == 'system') {
				//客服二维码
				$ftpWeb = config('app.ftp_web');
				$list['comp_ewm'] = $list['comp_ewm'] ? $ftpWeb.$list['comp_ewm'] : '';
				$list['kefu_ewm'] = $list['kefu_ewm'] ? $ftpWeb.$list['kefu_ewm'] : '';
				$list['simple_title'] = $list['simple_title'] ? $list['simple_title'] : 'DancePHP';
			}
			$configInfo = $list;
			cache($sys_identify, $configInfo, 3600);
		}
		return $configInfo;
	}
	
	public function getConfigsBySpace($config_space) {
		$data = $this->where(array('config_space'=>$config_space))->select();
		$data = $data->toArray(); //转换为数组
		$list = array();
		foreach ($data as $key => $value) {
			$list[$value['config_name']] = $value['config_value'];
		}
		if ($config_space == 'system') {
			// //支付方式
			// $list['is_alipay'] = $list['allow_pay_method'] & 1 ? 1 : 0;
			// $list['is_wxpay'] = $list['allow_pay_method'] & 2 ? 1 : 0;
			// $list['is_balance'] = $list['allow_pay_method'] & 4 ? 1 : 0;
			// //客服二维码
			// $ftpWeb = config('app.ftp_web');
			// $list['kefu_ewm'] = $list['kefu_ewm'] ? $ftpWeb.$list['kefu_ewm'] : '';
		}
		return $list;
	}

	public function getConfigByNameAndSpace($config_name, $config_space) {
		$info = $this->where(array('config_name'=>$config_name, 'config_space'=>$config_space))->find();
		return $info;
	}

	public function getConfigValueByNameAndSpace($config_name, $config_space) {
		$info = $this->where(array('config_name'=>$config_name, 'config_space'=>$config_space))->find();
		return $info['config_value'];
	}

	public function updateConfigByNameAndSpace($config_name, $config_space, $data) {
		$result = $this->where(array('config_name'=>$config_name, 'config_space'=>$config_space))->data($data)->update();
		return $result;
	}

	public function countSearch($map) {
		return $this->where($map)->count();
	}

	public function search($map, $limit=0) {
		$data = $this->where($map)->order('create_time desc')->limit($offset, $limit)->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	protected function parseSearch($data) {
		$ftpWeb = config('app.ftp_web');

		$list = array();
		foreach ($data as $key => $value) {
			//$value['picurl'] = $value['picurl'] ? $ftpWeb.$value['picurl'] : '';
			//$value['create_time'] = $value['create_time'] ? date("Y-m-d H:i:s", $value['create_time']) : '';

			$list[$value['id']] = $value;
		}
		return array($list);
	}

}